package com.cloud.result;

public class Result<T> {
	
	Integer code;
    String desc;
    T body;

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public T getBody() {
        return body;
    }

    public void setBody(T body) {
        this.body = body;
    }

    void build(Integer code, String description) {
        this.code = code;
        this.desc = description;
    }

    public Result<T> setSuccess(T t) {
        build(0, "成功");
        this.body = t;
        return this;
    }

    public boolean isSucceed() {
        if ((code != null) && (code == 0)) {
            return true;
        }
        return false;
    }

    public void setFailure(Integer code, String desc) {
        build(code, desc);
    }

    public void setException(String desc) {
        build(-2, desc);
    }

    public void setException(Throwable e) {
        setException(e.toString());
    }

    public enum GateErrCode {
        INVALID_PARAMETER(10000),
        INVALID_TOKEN(10001),
        MAX_CODE(19999);

        Integer code;
        GateErrCode(Integer nCode) {
            this.code = nCode;
        }

        public Integer value() {
            return this.code;
        }
    }

    public Result setFailure(GateErrCode code, String desc) {
        build(code.value(), desc);
        return this;
    }
}
